# -*- coding: utf-8 -*-

from PyQt4 import Qt, QtGui, QtCore

from Ui_FeOptions import Ui_FeOptions
from MovieData import *

class FeOptions(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_FeOptions()
        self.ui.setupUi(self)

        self.connect(self.ui.btnFont, QtCore.SIGNAL('clicked()'), self.fontDialog)
        self.connect(self.ui.btnOk, QtCore.SIGNAL('clicked()'), self.fromScreenToData)
        self.connect(self.ui.btnApply, QtCore.SIGNAL('clicked()'), self.fromScreenToData)
        self.connect(self.ui.btnOk, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('accept()'))
        self.connect(self.ui.btnCancel, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('reject()'))

        self.fromDataToScreen()

    def fontDialog(self):
        dialog = QtGui.QFontDialog()
        dialog.setCurrentFont(QtGui.qApp.font())
        if dialog.exec_():
            self.ui.btnFont.setFont(dialog.currentFont())

    def fromScreenToData(self):
        uniqueColumns = []
        if self.ui.chkName.checkState() == Qt.Qt.Checked:
            uniqueColumns.append(Columns.name)
        if self.ui.chkYear.checkState() == Qt.Qt.Checked:
            uniqueColumns.append(Columns.year)
        if self.ui.chkDirector.checkState() == Qt.Qt.Checked:
            uniqueColumns.append(Columns.director)
        Options.uniqueColumns = uniqueColumns

        QtGui.qApp.setFont(self.ui.btnFont.font())
        Options.save()


    def fromDataToScreen(self):
        setCheck(self.ui.chkName, Columns.name in Options.uniqueColumns)
        setCheck(self.ui.chkYear, Columns.year in Options.uniqueColumns)
        setCheck(self.ui.chkDirector, Columns.director in Options.uniqueColumns)


# helper functions
def setCheck(chk, value):
    if value:
        chk.setCheckState(Qt.Qt.Checked)
    else:
        chk.setCheckState(Qt.Qt.Unchecked)


if __name__ == "__main__":
    app = QtGui.QApplication([])
    Options.load()
    dialog = FeOptions()
    dialog.show()
    app.exec_()
